import _ from 'lodash'

const lazyRouter = {
  routes: [],
  add(route, name, redirect, meta, component, children, ...args) {
    /**
     * route 路由地址
     * name 路由名称
     * meta
     * component 视图路径，用,或者/分割，如article.index,则视图文件地址为article/index.vue
     * children 子菜单
     */
    if (!_.isEmpty(children)) {
      redirect = _.isEmpty(redirect) ? (children[0].path || '') : redirect
    }
    if (args.length) {
      args.forEach(item => {
        meta = _.merge(meta, item)
      })
    }
    const _router = {
      path: route,
      name,
      meta,
      component,
      children
    }
    if (!_.isEmpty(redirect)) {
      _router.redirect = redirect
    }

    this.routes.push(_router)
  }
}
export default lazyRouter
